/*
 * Copyright(C) 2010 the original author or authors.
 *
 * Change History Log
 * ----------------------------------------------------
 * Version: 1.0
 * Date: 2011-5-22
 * Author: wangzl
 * ----------------------------------------------------
 */
package com.seemsea.web.admin.coupon;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.seemsea.entity.admin.coupon.Coupon;
import com.seemsea.entity.admin.coupon.CouponItem;
import com.seemsea.entity.admin.coupon.Product;
import com.seemsea.service.admin.CouponService;
import com.seemsea.service.admin.ProductService;

/**
 * 
 * 
 * @author wangzl
 * @since 2011-5-22
 */
@Controller
public class CouponController {
	@Autowired
	private CouponService couponService;
	@Autowired
	private ProductService productService;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}
	
	@RequestMapping(value="/admin/coupon/couponManage.nhtml")
    public String index(Model model,HttpServletRequest request) {
		List<Coupon> coupons = couponService.getAll();
		model.addAttribute("coupons", coupons);
        return "/admin/coupon/couponManage";
    }
	
	@RequestMapping(value="/admin/coupon/couponAdd.nhtml")
    public String add(CouponForm couponForm,Model model,HttpServletRequest request) {
		String couponCode = couponForm.getCoupon().getCode();
		if(couponCode!=null && !"".equals(couponCode)){
			couponForm.setAction("update");
			Coupon coupon = couponService.get(couponCode);
			couponForm.setCoupon(coupon);
			List<CouponItem> couponItems = coupon.getCouponItems();
			if(couponItems != null && couponItems.size() > 0){
				couponForm.setItemsNum(couponItems.size());
			}
		}
		List<Product> productsList = productService.getAll();
		model.addAttribute("productsList", productsList);
        return "/admin/coupon/couponAdd";
    }
	
	@RequestMapping(value="/admin/coupon/couponSave.nhtml")
    public String save(CouponForm couponForm,Model model,HttpServletRequest request) {
		Coupon coupon = couponForm.getCoupon();
		List<CouponItem> couponItems = coupon.getCouponItems();
		for(Iterator<CouponItem> iter = couponItems.iterator();iter.hasNext();){
			CouponItem tmpCouponItem = iter.next();
			if(tmpCouponItem != null){
				tmpCouponItem.setCoupon(coupon);
			}
		}
		String action = couponForm.getAction();
		if(action.equals("update")){
			couponForm.getCoupon().setUpdateTime(new Date());
		}else{
			couponForm.getCoupon().setCreateTime(new Date());
			couponForm.getCoupon().setUpdateTime(new Date());
		}
		couponService.operateCoupon(coupon);
        return "redirect:/admin/coupon/couponAdd.nhtml";
    }	
	
/*	@RequestMapping(value="/admin/coupon/couponDeleteItem.nhtml")
	public void DeleteCouponItem( HttpServletRequest request,HttpServletResponse response) {
		String code =  request.getParameter("code");
		if(code != null){
			CouponItem couponItem = couponService.getVariable(Integer.valueOf(ID));
			variableService.deleteVariable(variable);
		}
	}*/
}
